package com.yys.springcloud.controller;

import com.yys.springcloud.entities.CommonResult;
import com.yys.springcloud.entities.Payment;
import com.yys.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @program: cloudalibaba2021
 * @description:
 * @author: Mr.Wu
 * @create: 2021-06-21 15:06
 **/
@RestController
@Slf4j
@RefreshScope // 支持Nacos的动态刷新功能
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @Value("${user.name}")
    private String userName;

    @PostMapping("/payment/create")
    public CommonResult create(@RequestBody Payment payment){

        int result = paymentService.create(payment);
        log.info("插入结果" + result);

        if(result > 0){
            return new CommonResult(200,"插入数据库成功" + serverPort,result);
        } else {
            return new CommonResult(444,"插入数据库失败" + serverPort,null);
        }
    }
    @GetMapping("/payment/get/{id}")
    public CommonResult getPaymentId(@PathVariable("id") Long id){
        Payment payment = paymentService.getPaymentId(id);
        log.info("payment:" + payment);
        if(payment != null){
            return new CommonResult(200,"获得数据成功" + serverPort,payment);
        } else {
            return new CommonResult(444,"获得数据失败" + serverPort,null);
        }
    }
    @GetMapping("/payment/getServerPort")
    public CommonResult getServerPort(){
        if(serverPort != null){
            return new CommonResult(200,"获得数据成功：" + userName);
        } else {
            return new CommonResult(444,"获得数据失败：" + userName);
        }
    }
}
